package com.hnu.store.controller;

import com.hnu.store.entity.News;
import com.hnu.store.service.INewsService;
import com.hnu.store.utils.JsonResult;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@CrossOrigin
/*
新闻的控制层
 */
@RequestMapping("news")
public class NewsController extends BaseController {

  @Autowired
  private INewsService iNewsService;

  //  添加新闻
  @RequestMapping("addNews")
  public JsonResult addNews(News news, MultipartFile multipartFile) {
    iNewsService.addNews(news, multipartFile);

    return new JsonResult(OK);
  }

  //  删除新闻
  @RequestMapping("deleteNews")
  public JsonResult deleteNews(String id) {
    iNewsService.deleteNews(id);

    return new JsonResult(OK);
  }

  //  编辑新闻
  @RequestMapping("editNews")
  public JsonResult editNews(News news, MultipartFile multipartFile) {
    iNewsService.editNews(news, multipartFile);

    return new JsonResult(OK);
  }

  //  获取新闻(管理员用)
  @RequestMapping("getNews")
  public JsonResult getNews() {
    ArrayList<News> newsList = iNewsService.getNews();
    JsonResult result = new JsonResult(OK);
    result.addData("newsList", newsList);
    return result;
  }

  //  获取新闻封面
  @RequestMapping("getImg")
  void getImg(String filePath,String fileName, HttpServletResponse response) {
      iNewsService.getImg(filePath,fileName,response);
  }

  /**
   * 展示新闻（随机从新闻列表中展示5个）
   * @return
   */
  @RequestMapping("showNews")
  public JsonResult showNews() {
    ArrayList<News> newsList = iNewsService.showNews();
    JsonResult result = new JsonResult(OK);
    result.addData("newsList", newsList);
    return result;
  }

}
